package com.example.module_home.DB;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.module_home.Public.Constant;
import com.example.module_home.Public.ITHouseTables;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * Created by 陈煜 on 2018/4/15.
 */

public class DBOrmLiteHelper extends OrmLiteSqliteOpenHelper {
    private DBOrmLiteHelper(Context context) {
        super(context, Constant.DATABASE_NAME, null, 1);
    }
    private static DBOrmLiteHelper sHelper = null;
    private Class mDataClass;
    public static synchronized DBOrmLiteHelper getInstance(Context context){
        if(sHelper==null){
            sHelper = new DBOrmLiteHelper(context);
        }
        return sHelper;
    }

    public void onCreateTable(Class dataClass) throws SQLException {
        this.mDataClass = dataClass;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            //OrmLite框架中自带的工具类
            TableUtils.createTable(connectionSource, mDataClass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
//        try {
//            //删除表
//            TableUtils.dropTable(connectionSource,mDataClass,true);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        //再创建表
        onCreate(sqLiteDatabase,connectionSource);
    }

}
